const Location = require('../models/Location');
const warehouseConfig = require('../config/warehouseConfig');

class LocationController {
  // 获取所有货位
  static getAllLocations(req, res) {
    Location.findAll((err, locations) => {
      if (err) {
        return res.status(500).json({
          success: false,
          message: '获取货位列表失败',
          error: err.message
        });
      }
      
      res.json({
        success: true,
        data: locations
      });
    });
  }

  // 根据ID获取货位
  static getLocationById(req, res) {
    const { id } = req.params;
    
    Location.findById(id, (err, location) => {
      if (err) {
        return res.status(500).json({
          success: false,
          message: '查询货位失败',
          error: err.message
        });
      }
      
      if (!location) {
        return res.status(404).json({
          success: false,
          message: '货位未找到'
        });
      }
      
      res.json({
        success: true,
        data: location
      });
    });
  }

  // 根据货位编码获取货位
  static getLocationByCode(req, res) {
    const { code } = req.params;
    
    Location.findByLocationCode(code, (err, location) => {
      if (err) {
        return res.status(500).json({
          success: false,
          message: '查询货位失败',
          error: err.message
        });
      }
      
      if (!location) {
        return res.status(404).json({
          success: false,
          message: '货位未找到'
        });
      }
      
      res.json({
        success: true,
        data: location
      });
    });
  }

  // 创建新货位
  static createLocation(req, res) {
    const {
      locationCode, aisle, row, column, level, status
    } = req.body;
    
    // 验证必填字段
    if (!locationCode || !aisle || !row || !column || !level) {
      return res.status(400).json({
        success: false,
        message: '货位编码、巷道、排、列、层为必填项'
      });
    }
    
    const newLocation = new Location(
      null,
      locationCode,
      aisle,
      row,
      column,
      level,
      status || 'enabled'
    );
    
    // 保存货位
    newLocation.save((err, result) => {
      if (err) {
        return res.status(500).json({
          success: false,
          message: '创建货位失败',
          error: err.message
        });
      }
      
      res.status(201).json({
        success: true,
        message: '货位创建成功',
        data: { id: result.id, ...newLocation }
      });
    });
  }

  // 更新货位状态
  static updateLocationStatus(req, res) {
    const { id } = req.params;
    const { status } = req.body;
    
    // 验证状态值
    if (!['enabled', 'disabled'].includes(status)) {
      return res.status(400).json({
        success: false,
        message: '状态值必须是enabled或disabled'
      });
    }
    
    Location.updateStatus(id, status, (err, result) => {
      if (err) {
        return res.status(500).json({
          success: false,
          message: '更新货位状态失败',
          error: err.message
        });
      }
      
      if (!result.updated) {
        return res.status(404).json({
          success: false,
          message: '货位未找到'
        });
      }
      
      res.json({
        success: true,
        message: '货位状态更新成功'
      });
    });
  }

  // 批量更新货位状态
  static updateLocationStatusBatch(req, res) {
    const { ids, status } = req.body;
    
    // 验证参数
    if (!Array.isArray(ids) || ids.length === 0) {
      return res.status(400).json({
        success: false,
        message: '必须提供货位ID数组'
      });
    }
    
    if (!['enabled', 'disabled'].includes(status)) {
      return res.status(400).json({
        success: false,
        message: '状态值必须是enabled或disabled'
      });
    }
    
    Location.updateStatusBatch(ids, status, (err, result) => {
      if (err) {
        return res.status(500).json({
          success: false,
          message: '批量更新货位状态失败',
          error: err.message
        });
      }
      
      res.json({
        success: true,
        message: `成功更新${result.updated}个货位状态`,
        data: result
      });
    });
  }

  // 根据查询条件获取货位
  static getLocationsByQuery(req, res) {
    const queryParams = req.query;
    
    Location.findByQuery(queryParams, (err, locations) => {
      if (err) {
        return res.status(500).json({
          success: false,
          message: '查询货位列表失败',
          error: err.message
        });
      }
      
      res.json({
        success: true,
        data: locations
      });
    });
  }

  // 删除货位
  static deleteLocation(req, res) {
    const { id } = req.params;
    
    Location.delete(id, (err, result) => {
      if (err) {
        return res.status(500).json({
          success: false,
          message: '删除货位失败',
          error: err.message
        });
      }
      
      if (!result.deleted) {
        return res.status(404).json({
          success: false,
          message: '货位未找到'
        });
      }
      
      res.json({
        success: true,
        message: '货位删除成功'
      });
    });
  }

  // 获取仓库配置信息
  static getWarehouseConfig(req, res) {
    res.json({
      success: true,
      data: warehouseConfig
    });
  }

  // 更新仓库配置信息
  static updateWarehouseConfig(req, res) {
    // 这里可以实现配置更新逻辑
    // 为了简化，我们只返回当前配置
    res.json({
      success: true,
      message: '仓库配置信息获取成功',
      data: warehouseConfig
    });
  }
}

module.exports = LocationController;